

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="老王">
  <meta name="keywords" content="">
  
    <meta name="description" content="书接上文，我们已经了解 shiny 的主体结构，今天详细讲一讲用户界面（ui）部分。ui 是 shiny web 应用展示给用户的界面，那如何控制 ui 的布局并向其中插入图片、表格、文字等各种元素呢？ 布局 shiny使用 fluidPage 函数创建 ui 布局。 12345678ui &lt;- fluidPage(  titlePanel(&quot;title panel&quot;),">
<meta property="og:type" content="article">
<meta property="og:title" content="R语言 | shiny 基础（一）：shiny 用户界面（UI）">
<meta property="og:url" content="https://laowang2023.cn/2023/06/02/20230602-shinyBasics2UI/index.html">
<meta property="og:site_name" content="请叫我老王">
<meta property="og:description" content="书接上文，我们已经了解 shiny 的主体结构，今天详细讲一讲用户界面（ui）部分。ui 是 shiny web 应用展示给用户的界面，那如何控制 ui 的布局并向其中插入图片、表格、文字等各种元素呢？ 布局 shiny使用 fluidPage 函数创建 ui 布局。 12345678ui &lt;- fluidPage(  titlePanel(&quot;title panel&quot;),">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://laowang2023.cn/img/index_banner/shinyLogo.png">
<meta property="article:published_time" content="2023-06-02T03:46:15.000Z">
<meta property="article:modified_time" content="2025-06-02T05:15:01.226Z">
<meta property="article:author" content="老王">
<meta property="article:tag" content="R">
<meta property="article:tag" content="shiny">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://laowang2023.cn/img/index_banner/shinyLogo.png">
  
  
  
  <title>R语言 | shiny 基础（一）：shiny 用户界面（UI） - 请叫我老王</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"laowang2023.cn","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"2gDQrSMhKFxALJaFFpTbgc23-gzGzoHsz","app_key":"NBsdUEdxwrnzaL1XcLReQM4N","server_url":"https://2gdqrsmh.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":true}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/atom.xml" title="请叫我老王" type="application/atom+xml">
</head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>请叫我老王</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/atom.xml">
                <i class="iconfont icon-rss-fill"></i>
                <span>订阅</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/index_banner/shinyLogo.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="R语言 | shiny 基础（一）：shiny 用户界面（UI）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-06-02 11:46" pubdate>
          2023年6月2日 中午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.6k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          30 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">R语言 | shiny 基础（一）：shiny 用户界面（UI）</h1>
            
            
              <div class="markdown-body">
                
                <p><a href="http://laowang2023.cn/2023/05/31/20230531-shinyBasics1/">书接上文</a>，我们已经了解 shiny 的主体结构，今天详细讲一讲用户界面（ui）部分。ui 是 shiny web 应用展示给用户的界面，那如何控制 ui 的布局并向其中插入图片、表格、文字等各种元素呢？</p>
<h2 id="布局">布局</h2>
<p>shiny使用 <code>fluidPage</code> 函数创建 ui 布局。</p>
<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs R">ui <span class="hljs-operator">&lt;-</span> fluidPage<span class="hljs-punctuation">(</span><br>  titlePanel<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;title panel&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br><br>  sidebarLayout<span class="hljs-punctuation">(</span><br>    sidebarPanel<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;sidebar panel&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    mainPanel<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;main panel&quot;</span><span class="hljs-punctuation">)</span><br>  <span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>
<p><img src="sidebar-layout1.png" srcset="/img/loading.gif" lazyload alt="sidebar 布局" title="sidebar 布局"></p>
<p>在上面的例子中，首先使用 <code>titlePanel</code> 创建 ui 的标题栏，然后使用 <code>sidebarLayout</code> 将 ui 分为两个部分：侧边栏（<code>sidebarPanel</code>）和主体部分（<code>mainPanel</code>）。当然，除了 <code>titlePanel</code> 和 <code>sidebarLayout</code> 外，还有<a target="_blank" rel="noopener" href="https://shiny.posit.co/articles/layout-guide.html">其函数可以创建跟高级的 ui 布局</a>。</p>
<h2 id="向-ui-插入各种元素">向 ui 插入各种元素</h2>
<p>在上面的例子中我们把 ui 页面分为两个部分：sidebar panel 和 main panel，下一步就是向其中插入图片、表格或文字等内容，实现方法是把各种元素放在<code>*Panel</code>函数内，例如我们向 <code>sidebarPanel</code> 函数提供一个字符串参数“sidebar panel”，这一字符串将显示在 sidebar panel 内。</p>
<h3 id="HTML-元素">HTML 元素</h3>
<p>HTML（HyperText Markup Language）即超文本标记语言，是一种用于创建网页的标准标记语言，HTML 提供了一系列标签用来向网页内插入标题、文字、图片等。于此对应，在 shiny 中也有相应的函数向 ui 中插入各种元素。以下表格位 shiny 和 HTML 标签对应函数和功能，并举例说明使用方法。</p>
<table>
<thead>
<tr>
<th>shiny 函数</th>
<th>对应 HTML5 标签</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>p</code></td>
<td><code>&lt;p&gt;</code></td>
<td>一个文本段落</td>
</tr>
<tr>
<td><code>h1</code></td>
<td><code>&lt;h1&gt;</code></td>
<td>一级标题</td>
</tr>
<tr>
<td><code>h2</code></td>
<td><code>&lt;h2&gt;</code></td>
<td>二级标题</td>
</tr>
<tr>
<td><code>h3</code></td>
<td><code>&lt;h3&gt;</code></td>
<td>三级标题</td>
</tr>
<tr>
<td><code>h4</code></td>
<td><code>&lt;h4&gt;</code></td>
<td>四级标题</td>
</tr>
<tr>
<td><code>h5</code></td>
<td><code>&lt;h5&gt;</code></td>
<td>五级标题</td>
</tr>
<tr>
<td><code>h6</code></td>
<td><code>&lt;h6&gt;</code></td>
<td>六级标题</td>
</tr>
<tr>
<td><code>a</code></td>
<td><code>&lt;a&gt;</code></td>
<td>插入超链接</td>
</tr>
<tr>
<td><code>br</code></td>
<td><code>&lt;br&gt;</code></td>
<td>换行符</td>
</tr>
<tr>
<td><code>div</code></td>
<td><code>&lt;div&gt;</code></td>
<td>定义文档中的分区或节</td>
</tr>
<tr>
<td><code>span</code></td>
<td><code>&lt;span&gt;</code></td>
<td>组合文档中的行内元素</td>
</tr>
<tr>
<td><code>pre</code></td>
<td><code>&lt;pre&gt;</code></td>
<td>定义预格式化的文本</td>
</tr>
<tr>
<td><code>code</code></td>
<td><code>&lt;code&gt;</code></td>
<td>格式化的代码块</td>
</tr>
<tr>
<td><code>img</code></td>
<td><code>&lt;img&gt;</code></td>
<td>插入一张图片</td>
</tr>
<tr>
<td><code>strong</code></td>
<td><code>&lt;strong&gt;</code></td>
<td>强调文本，黑体</td>
</tr>
<tr>
<td><code>em</code></td>
<td><code>&lt;em&gt;</code></td>
<td>强调文本，斜体</td>
</tr>
<tr>
<td><code>HTML</code></td>
<td></td>
<td>直接将字符串作为 HTML 代码传递</td>
</tr>
</tbody>
</table>
<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>shiny<span class="hljs-punctuation">)</span><br><br><span class="hljs-comment"># Define UI ----</span><br>ui <span class="hljs-operator">&lt;-</span> fluidPage<span class="hljs-punctuation">(</span><br>  titlePanel<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;My Shiny App&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>  sidebarLayout<span class="hljs-punctuation">(</span><br>    sidebarPanel<span class="hljs-punctuation">(</span><br>      h2<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Installation&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      p<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Shiny is available on CRAN, so you can install it in the usual way from your R console:&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      code<span class="hljs-punctuation">(</span><span class="hljs-string">&#x27;install.packages(&quot;shiny&quot;)&#x27;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      img<span class="hljs-punctuation">(</span>src <span class="hljs-operator">=</span> <span class="hljs-string">&quot;rstudio.png&quot;</span><span class="hljs-punctuation">,</span> height <span class="hljs-operator">=</span> <span class="hljs-number">70</span><span class="hljs-punctuation">,</span> width <span class="hljs-operator">=</span> <span class="hljs-number">200</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-string">&quot;Shiny is a product of &quot;</span><span class="hljs-punctuation">,</span> <br>      span<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;RStudio&quot;</span><span class="hljs-punctuation">,</span> style <span class="hljs-operator">=</span> <span class="hljs-string">&quot;color:blue&quot;</span><span class="hljs-punctuation">)</span><br>    <span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    mainPanel<span class="hljs-punctuation">(</span><br>      h1<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Introducing Shiny&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      p<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Shiny is a new package from RStudio that makes it &quot;</span><span class="hljs-punctuation">,</span> <br>        em<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;incredibly easy &quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <br>        <span class="hljs-string">&quot;to build interactive web applications with R.&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      p<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;For an introduction and live examples, visit the &quot;</span><span class="hljs-punctuation">,</span><br>        a<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Shiny homepage.&quot;</span><span class="hljs-punctuation">,</span> <br>          href <span class="hljs-operator">=</span> <span class="hljs-string">&quot;http://shiny.rstudio.com&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      br<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      h2<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Features&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      p<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;- Build useful web applications with only a few lines of code—no JavaScript required.&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      p<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;- Shiny applications are automatically &#x27;live&#x27; in the same way that &quot;</span><span class="hljs-punctuation">,</span> <br>        strong<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;spreadsheets&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot; are live. Outputs change instantly as users modify inputs, without requiring a reload of the browser.&quot;</span><span class="hljs-punctuation">)</span><br>    <span class="hljs-punctuation">)</span><br>  <span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br><br><span class="hljs-comment"># Define server logic ----</span><br>server <span class="hljs-operator">&lt;-</span> <span class="hljs-keyword">function</span><span class="hljs-punctuation">(</span>input<span class="hljs-punctuation">,</span> output<span class="hljs-punctuation">)</span> <span class="hljs-punctuation">&#123;</span><br>  <br><span class="hljs-punctuation">&#125;</span><br><br><span class="hljs-comment"># Run the app ----</span><br>shinyApp<span class="hljs-punctuation">(</span>ui <span class="hljs-operator">=</span> ui<span class="hljs-punctuation">,</span> server <span class="hljs-operator">=</span> server<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>
<p><img src="my-shiny-app.png" srcset="/img/loading.gif" lazyload alt="向shiny app中插入HTML元素" title="向shiny app中插入HTML元素"></p>
<p>在上述例子中在 <code>*Panel</code> 函数中使用 <code>h2</code>、<code>p</code>、<code>em</code>、<code>img</code> 等函数向 ui 中的对应 panel 插入二级标题标题、段落、斜体文本、图片等元素。</p>
<h3 id="添加控制部件（widgets）">添加控制部件（widgets）</h3>
<p>shiny 可以构建一个交互式的 web 应用，用户如何与之交互呢？shiny 提供了一系列控制部件函数，用户使用这些部件可以向 web 应用发送信息，shiny 以此信息实时动态的调整输出。下面表格中是常用的控制部件函数，并举例说明其实用方法。</p>
<table>
<thead>
<tr>
<th>function</th>
<th>widget</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>actionButton</code></td>
<td>动作按钮</td>
</tr>
<tr>
<td><code>checkboxGroupInput</code></td>
<td>一组复选框</td>
</tr>
<tr>
<td><code>checkboxInput</code></td>
<td>单个复选框</td>
</tr>
<tr>
<td><code>dateInput</code></td>
<td>帮助选择日期的日历</td>
</tr>
<tr>
<td><code>dateRangeInput</code></td>
<td>一对用于选择日期范围的日历</td>
</tr>
<tr>
<td><code>fileInput</code></td>
<td>上传文件</td>
</tr>
<tr>
<td><code>helpText</code></td>
<td>可以添加到输入表单中的帮助文本</td>
</tr>
<tr>
<td><code>numericInput</code></td>
<td>数字输入框</td>
</tr>
<tr>
<td><code>radioButtons</code></td>
<td>一组单选按钮</td>
</tr>
<tr>
<td><code>selectInput</code></td>
<td>创建选择列表</td>
</tr>
<tr>
<td><code>sliderInput</code></td>
<td>滑动条</td>
</tr>
<tr>
<td><code>submitButton</code></td>
<td>提交按钮</td>
</tr>
<tr>
<td><code>textInput</code></td>
<td>文本输入框</td>
</tr>
</tbody>
</table>
<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs R">ui <span class="hljs-operator">&lt;-</span> fluidPage<span class="hljs-punctuation">(</span><br>  titlePanel<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;censusVis&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>  <br>  sidebarLayout<span class="hljs-punctuation">(</span><br>    sidebarPanel<span class="hljs-punctuation">(</span><br>      helpText<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Create demographic maps with </span><br><span class="hljs-string">               information from the 2010 US Census.&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      <br>      selectInput<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;var&quot;</span><span class="hljs-punctuation">,</span> <br>                  label <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Choose a variable to display&quot;</span><span class="hljs-punctuation">,</span><br>                  choices <span class="hljs-operator">=</span> <span class="hljs-built_in">list</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Percent White&quot;</span><span class="hljs-punctuation">,</span> <br>                                 <span class="hljs-string">&quot;Percent Black&quot;</span><span class="hljs-punctuation">,</span><br>                                 <span class="hljs-string">&quot;Percent Hispanic&quot;</span><span class="hljs-punctuation">,</span> <br>                                 <span class="hljs-string">&quot;Percent Asian&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                  selected <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Percent White&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>      <br>      sliderInput<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;range&quot;</span><span class="hljs-punctuation">,</span> <br>                  label <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Range of interest:&quot;</span><span class="hljs-punctuation">,</span><br>                  <span class="hljs-built_in">min</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span> <span class="hljs-built_in">max</span> <span class="hljs-operator">=</span> <span class="hljs-number">100</span><span class="hljs-punctuation">,</span> value <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span> <span class="hljs-number">100</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>    <span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    <br>    mainPanel<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><br>  <span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>
<p><img src="censusviz.png" srcset="/img/loading.gif" lazyload alt="控制部件" title="向ui中添加控制部件"></p>
<p>如上面例子所示，和插入 HTML 元素类似，控制部件同样是在 <code>*Panel</code> 函数中使用。控制部件的第一个参数为字符串，表明这个部件的<strong>名字</strong>，我们使用这个参数可以获得部件的值。第二个参数为部件的<strong>标签</strong>，可以在 ui 中看。其他的参数根据部件不同的功能而不同。</p>
<hr>
<p class="note note-primary">参考资料</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://shiny.posit.co/r/getstarted/shiny-basics/lesson2/">https://shiny.posit.co/r/getstarted/shiny-basics/lesson2/</a></li>
<li><a target="_blank" rel="noopener" href="https://shiny.posit.co/r/getstarted/shiny-basics/lesson2/">https://shiny.posit.co/r/getstarted/shiny-basics/lesson2/</a></li>
</ul>
<p><img src="https://images.pexels.com/photos/20692994/pexels-photo-20692994.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=600&amp;lazy=load" srcset="/img/loading.gif" lazyload alt="图片与主题无关" title="图片与主题无关"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" class="category-chain-item">技术分享</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/R/">#R</a>
      
        <a href="/tags/shiny/">#shiny</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>R语言 | shiny 基础（一）：shiny 用户界面（UI）</div>
      <div>https://laowang2023.cn/2023/06/02/20230602-shinyBasics2UI/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>老王</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年6月2日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/06/27/20230627-NewCircos/" title="R绘图 | 如何改变circos图中不同track中sector间的比例">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">R绘图 | 如何改变circos图中不同track中sector间的比例</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/05/31/20230531-shinyBasics1/" title="R语言 | shiny基础（一）：shiny入门">
                        <span class="hidden-mobile">R语言 | shiny基础（一）：shiny入门</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"81Ynz10f9eqlTLw0V6ue4tBu-gzGzoHsz","appKey":"4Mxe0RlrV6wJ0n9C6ZDpYI6O","path":"window.location.pathname","placeholder":"你是不是想说点什么","avatar":"identicon","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":true,"serverURLs":"https://81ynz10f.lc-cn-n1-shared.com","emojiCDN":null,"emojiMaps":null,"enableQQ":true},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/js/leancloud.js" ></script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
